package cn.highedu.coolshark.product.service.impl;

import cn.highedu.coolshark.common.*;
import cn.highedu.coolshark.product.mapper.AlbumMapper;
import cn.highedu.coolshark.product.mapper.PictureMapper;
import cn.highedu.coolshark.product.pojo.dto.AlbumAddNewDTO;
import cn.highedu.coolshark.product.pojo.dto.PictureAddNewDTO;
import cn.highedu.coolshark.product.pojo.entity.Album;
import cn.highedu.coolshark.product.pojo.entity.Picture;
import cn.highedu.coolshark.product.pojo.vo.AlbumStandardVO;
import cn.highedu.coolshark.product.pojo.vo.PictureListItemVO;
import cn.highedu.coolshark.product.pojo.vo.PictureStandardVO;
import cn.highedu.coolshark.product.service.IAlbumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 相册的业务类
 */
@Service
@Slf4j
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;
    /**
     * 根据相册id查询相册的全部图片
     * @param albumId 相册id
     * @return 相册的全部图片
     */
    @Override
    public List<PictureListItemVO> getPictureListByAlbumId(Long albumId) {
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "查询相册列表失败, 相册数据不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        List<PictureListItemVO> list = pictureMapper.listByAlbumId(albumId);
        return list;
    }

    /**
     * 添加相册
     * @param albumAddNewDTO 相册数据
     * @return 新增的相册的id
     */
    @Override
    public Long addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的业务, 参数: {}", albumAddNewDTO);
        // 获取待添加相册的名称
        String name = albumAddNewDTO.getName();
        // 查询数据库表中是否有相同名字的相册
        int count = albumMapper.countByName(name);
        log.debug("根据名称【{}】统计数量, 结果", name, count);
        if (count > 0) {
            // 有相同名字的相册, 抛出异常
            String message = "添加相册失败, 相册名称已被占用";
            log.warn(message);
            throw new EntityAlreadyExistsException(message);
        }
        // 创建相册对象(Album)
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO, album);
        log.debug("即将执行插入相册数据, 参数: {}", album);
        int rows = albumMapper.insert(album);
        if (rows != 1) {
            String message = "添加相册失败, 服务器忙, 请稍后再试";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
        return album.getId();
    }

    /**
     * 为相册添加图片
     * @param pictureAddNewDTO
     */
    @Override
    public void addPicture(PictureAddNewDTO pictureAddNewDTO) {
        AlbumStandardVO album = albumMapper.getStandardById(pictureAddNewDTO.getAlbumId());
        if (album == null) {
            String message = "添加图片失败, 相册数据不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        Picture picture = new Picture();
        BeanUtils.copyProperties(pictureAddNewDTO, picture);
        picture.setGmtCreate(LocalDateTime.now());
        picture.setGmtModified(LocalDateTime.now());
        picture.setDescription("暂无描述");
        picture.setIsCover(0);
        picture.setSort(0);
        int rows = pictureMapper.insert(picture);
        if (rows != 1) {
            String message = "添加图片失败, 服务器忙, 请稍后再试";
            log.warn(message);
            throw new EntitySaveFailedException(message);
        }
    }

    /**
     * 设置相册封面图片
     * @param albumId   相册ID
     * @param pictureId 图片ID
     */
    @Override
    public void setCover(Long albumId, Long pictureId) {
        log.debug("开始处理【设置相册封面】的业务, 参数: {}, {}", albumId, pictureId);
        // 查询相册对象
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "设置相册封面失败, 相册数据不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        // 将相册中的所有图片设置为非封面
        pictureMapper.clearCoverByAlbumId(albumId);
        // 把指定的图片设置为相册的封面
        Picture picture = new Picture();
        picture.setId(pictureId);
        picture.setIsCover(1); // 设置为封面
        picture.setGmtModified(LocalDateTime.now());
        // 修改图片数据
        int rows = pictureMapper.update(picture);
        if (rows != 1) {
            String message = "设置相册封面失败, 没有指定的图片";
            log.warn(message);
            throw new EntityUpdateFailedException(message);
        }
    }

    /**
     * 删除相册中的图片
     *
     * @param albumId   相册ID
     * @param pictureId 图片ID
     */
    @Override
    public void deletePicture(Long albumId, Long pictureId) {
        log.debug("开始处理【删除相册图片】的业务, 参数: {}, {}", albumId, pictureId);
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null) {
            String message = "删除图片失败, 相册数据不存在";
            log.warn(message);
            throw new EntityNotFoundException(message);
        }
        PictureStandardVO pictureStandardVO = pictureMapper.getStandardById(pictureId);
        // 删除图片
        int rows = pictureMapper.deleteById(pictureId);
        if (rows != 1) {
            String message = "删除图片失败, 服务器忙, 请稍后再试";
            log.warn(message);
            throw new EntityDeleteFailedException(message);
        }
        // 判断删除的图片是不是封面
        if (pictureStandardVO.getIsCover().equals(1)) {
            // 如果删除的图片是封面, 把相册中的第一张图片设置为封面
            PictureStandardVO firstPicture = pictureMapper.getFirstPictureByAlbumId(albumId);
            if (firstPicture != null) {
                Picture picture = new Picture();
                picture.setId(firstPicture.getId());
                picture.setIsCover(1);
                picture.setGmtModified(LocalDateTime.now());
                pictureMapper.update(picture);
            }
        }
    }
}
